package com

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import hotchemi.com.github.BitmapLruCache


// bitmap 缓存
object BitmapMemCache {

    val cache = BitmapLruCache(200)
    val res:Resources by lazy {
        App.app.resources
    }
    val opts:BitmapFactory.Options by lazy{
        val opt = BitmapFactory.Options()
        opt.inDensity = res.displayMetrics.densityDpi
        opt.inTargetDensity = res.displayMetrics.densityDpi
        opt
    }

    @JvmStatic
    fun initBitmap(resId: Int): Bitmap {
        val key = resId.toString()
        val bitmap = cache.get(key)
        if (bitmap != null){
            return bitmap
        }else{
            // Bitmap的配置参数
            val bitmap  = BitmapFactory.decodeResource(res, resId,opts)
            cache.put(key, bitmap)
            return bitmap
        }
    }

    @JvmStatic
    fun getBitmap(resId: Int): Bitmap = initBitmap(resId)
}